﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using GH.Model;
using GH.BLL;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
    }

    protected void btnLogin_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            string strUser = txtUser.Text;
            string strPassword = txtPassword.Text;
            if (strUser==string.Empty)
            {
                AlertMessage("用户名密码不正确");
                return;
            }
            用户信息 userInfo = new 用户信息();

            GH.BLL.User userbll = new User();

            int outId=0;
            if (userbll.UserExists(strUser,out outId))
            {
                userInfo = userbll.GetModel(outId);
                
                if (strUser==userInfo.用户ID&&strPassword==userInfo.登陆密码)
                {
                    if (userInfo.启用状态=="0")
                    {
                        AlertMessage("用户已被锁定");
                        JavascriptGoUrl("Login.aspx");
                    }
                    else
                    {
                        Session["UserID"] = userInfo.ID.ToString();
                        Session["UserName"] = userInfo.用户ID.ToString();
                        Session["Union"] = userInfo.单位.ToString();
                        Session["SelectCurrentYear"] = System.DateTime.Now.Year.ToString();
                        Session["UnionSelect"]= userInfo.单位.ToString();
                        JavascriptGoUrl("index.aspx");
                    }
                }
                else
                {
                    AlertMessage("用户名密码不正确");
                    JavascriptGoUrl("Login.aspx");
                }
            }
            else
            {
                AlertMessage("用户名不存在");
                JavascriptGoUrl("Login.aspx");
            }
            
        }
    }

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        txtUser.Text = string.Empty;
        txtPassword.Text = string.Empty;
    }

    /// <summary>
    /// 跳转到其他页面
    /// </summary>
    /// <param name="url">页面地址</param>7
    public void JavascriptGoUrl(string url)
    {
        string script = string.Format("<script>location = '{0}';</script>", url);
        ClientScript.RegisterStartupScript(this.GetType(), System.Guid.NewGuid().ToString(), script);
    }

    public void AlertMessage(string msg)
    {
        string script = "<script>alert('" + msg + "')</script>";
        ClientScript.RegisterStartupScript(this.GetType(), System.Guid.NewGuid().ToString(), script);
    }
}
